package com.wonking.springbootv2.typeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.wonking.springbootv2.enumeration.GenderEnum;


@MappedTypes(GenderEnum.class)
@MappedJdbcTypes(JdbcType.INTEGER)
public class GenderTypeHandler extends BaseTypeHandler<GenderEnum> {

	@Override
	public GenderEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
		int code = rs.getInt(columnName);
		return GenderEnum.getGenderEnum(code);
	}

	@Override
	public GenderEnum getNullableResult(ResultSet rs, int index) throws SQLException {
		int code = rs.getInt(index);
		return GenderEnum.getGenderEnum(code);
	}

	@Override
	public GenderEnum getNullableResult(CallableStatement cs, int index) throws SQLException {
		int code = cs.getInt(index);
		return GenderEnum.getGenderEnum(code);
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int index, GenderEnum gender, JdbcType jdbcType) throws SQLException {
		ps.setInt(index, gender.getCode());
	}

}
